package com.edu.app.controller.core;

import com.edu.app.bean.core.SysLesson;
import com.edu.app.bean.core.popup.LessonsPop;
import com.edu.app.constant.ErrorConstant;
import com.edu.app.constant.StaticConstant;
import com.edu.app.constant.SysMessage;
import com.edu.app.service.sys.core.SysLessonService;
import com.edu.app.util.ErrorUtil;
import com.edu.app.util.JWTUtil;
import com.edu.app.util.MsgUtil;

import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping(value = "/api/lessons")
@RestController
public class SysLessonController {

    @Autowired
    SysLessonService sysLessonService;



    /*
     * 查询User列表
     * http://localhost:8080/api/roles
     * */
    @GetMapping(value = "")
    @RequiresAuthentication
    public ModelMap getLessonList (){
        ModelMap map = new ModelMap();
        try{
            List<SysLesson> lessonList = sysLessonService.getObjectList();
            return MsgUtil.retMsg(SysMessage.GET_ROLE_LIST_SUCCEED, StaticConstant.LESSON_LIST, lessonList);
        }catch(Exception e){
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }
    }

    @GetMapping(value = "/{id}")
    public ModelMap getLessonById(@PathVariable Integer id){

        try{
            SysLesson sysLesson = sysLessonService.getObjectById(id);
            return MsgUtil.retMsg(SysMessage.GET_LESSON_SUCCEED, StaticConstant.LESSON, sysLesson);
        }catch(Exception e){
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }
    }

    /*
     * 按Page分页查询列表
     * 测试链接
     * http://localhost:8080/api/roles/pages?offset=1&pageSize=2
     * */
    @GetMapping("/pages")
    public ModelMap getLessonByPage(@RequestParam Integer offset, @RequestParam Integer limit) {

        try {
            PageInfo pageInfo = sysLessonService.findItemByPage(offset, limit);
            return MsgUtil.retMsg(SysMessage.GET_LESSON_BY_PAGE_SUCCEED, "page_info", pageInfo);
        } catch (Exception e) {
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }
    }

    //增加新lesson
    @PostMapping("")
    @RequiresAuthentication
    public ModelMap addLesson(@RequestHeader("Authorization") String token, @RequestBody SysLesson sysLesson){
        try{
            String username = JWTUtil.getUsername(token);
            sysLesson.initializeSystemColumns(username);

            Integer r = sysLessonService.add(sysLesson);
            if(r > 0){
                return MsgUtil.retMsg(SysMessage.ADD_LESSON_SUCCEED, StaticConstant.LESSON, sysLesson);
            }else{
                return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
            }
        }catch (org.springframework.dao.DuplicateKeyException e){
            return ErrorUtil.retErrMsg(ErrorConstant.ROLE_ALREADY_EXISTS);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

    @PutMapping("")
    public ModelMap update (@RequestBody SysLesson sysLesson){

        Integer i = sysLessonService.update(sysLesson);
        if(i > 0 ){
            return MsgUtil.retMsg(SysMessage.UPDATE_LESSON_SUCCEED, null, null);
        }
        else{
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }

    }


    @DeleteMapping("")
    @RequiresAuthentication
    public ModelMap delete (@RequestParam Integer id){
        try{
            //Before deleting the role, you have to clear up the intersection table firstly.
            //sysUserLessonService.deleteUserLessonByLessonId(id);
            sysLessonService.delete(id);
            return MsgUtil.retMsg(SysMessage.DELETE_ROLE_SUCCEED, null, null);
        }catch(Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }


    @GetMapping(value = "/lessonspop")
    //@RequiresRoles(value = {StaticConstant.ADMIN_ROLE})
    public ModelMap getUsersPop (){
        try{
            List<LessonsPop> lessonsPops = sysLessonService.getLessonsPop();
            return MsgUtil.retMsg(SysMessage.GET_LESSONPOPLIST_SUCCEED, "pop_list", lessonsPops);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }
    }

}
